#!/usr/bin/env python
from os import path
Import('build env module')

env.Append(CPPDEFINES=('KROLL_PHP_API_EXPORT', 1))
build.add_thirdparty(env, 'poco')

# PHP's headers are so messy that they flood the build with warnings 
# unless we turn off -Werror and try to suprress most warnings. This
# is bad news. At some point we should turn warnings back on.
if 'CPPFLAGS' in env:
	env['CPPFLAGS'] = env['CPPFLAGS'][:]
	if '-Werror' in env['CPPFLAGS']:
		env['CPPFLAGS'].remove('-Werror')
	if '-Wall' in env['CPPFLAGS']:
		env['CPPFLAGS'].remove('-Wall')
	env['CPPFLAGS'].append('-w')

env.Append(CPPPATH=[
	build.tp('php', 'include'),
	build.tp('php', 'include', 'TSRM'),
	build.tp('php', 'include', 'Zend'),
	build.tp('php', 'include', 'ext'),
	build.tp('php', 'include', 'main'),
	build.tp('php', 'include', 'sapi'),
])
env.Append(LIBPATH=[build.tp('php', 'lib')])
env.Append(CPPDEFINES=('ZTS', 1))

# On OS X PHP can only build for i386
if build.is_osx():
	env.Replace(CXX=['g++', '-arch', 'i386'])

if build.is_osx() or build.is_linux():
	env.Append(LIBS=['php5'])
	env.Append(CPPDEFINES=('PTHREADS', 1))

elif build.is_win32():
	env.Append(CPPDEFINES=('PHP_WIN32', 1))
	env.Append(CPPDEFINES=('ZEND_WIN32', 1))
	env.Append(CPPDEFINES=('_USE_32BIT_TIME_T', 1))
	env.Append(CPPDEFINES=('TSRM_WIN32', 1))
	#env.Append(CPPDEFINES=('ZEND_DEBUG', 0))
	env.Append(LIBS=['php5embed', 'php5ts'])

build.mark_build_target(env.SharedLibrary(
	path.join(module.dir, 'phpmodule'), Glob('*.cpp')))

if build.is_win32():
	build.utils.LightWeightCopy(build.tp('php', 'bin'), module.dir)
if build.is_osx() or build.is_linux():
	build.utils.LightWeightCopy(build.tp('php', 'lib'), module.dir)
